package com.tjnu.losca.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tjnu.losca.form.OrderManager;
import com.tjnu.losca.mapper.OrdeMapper;
import com.tjnu.losca.pojo.Orde;
import com.tjnu.losca.pojo.Ordergood;
import com.tjnu.losca.pojo.Userinfo;
import com.tjnu.losca.service.IOrdeService;
import com.tjnu.losca.service.IOrdergoodService;
import com.tjnu.losca.service.IUserinfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author losca
 * @since 2022-02-16
 */
@Service
public class OrdeServiceImpl extends ServiceImpl<OrdeMapper, Orde> implements IOrdeService {
    @Autowired
    IOrdergoodService ordergoodService;
    @Autowired
    IUserinfoService userinfoService;

    @Override
    public List<OrderManager> list(int pageNum, int pageSize, String search) {
        List<OrderManager> orderManagers = new ArrayList<>();
        QueryWrapper<Orde> wrapper = new QueryWrapper<Orde>();
        if(StringUtils.isNotBlank(search)) {
            wrapper.eq("orderNo",search);
        }
        List<Orde> list = this.list(wrapper);
        for (Orde orde : list) {
            Integer orderNo = orde.getOrderNo();
            QueryWrapper<Ordergood> ordergoodQueryWrapper = new QueryWrapper<>();
            ordergoodQueryWrapper.eq("orderNo", orderNo);
            Ordergood ordergood = ordergoodService.getOne(ordergoodQueryWrapper);
            Userinfo user = userinfoService.getById(orde.getHostid());
            OrderManager orderManager = new OrderManager();
            orderManager.setOrderNo(orderNo);
            orderManager.setProductName(ordergood.getProductName());
            orderManager.setStatus(orde.getStatus());
            orderManager.setStatusDesc(orde.getStatusDesc());
            orderManager.setTotalPrice(ordergood.getTotalPrice());
            orderManager.setImgurl(ordergood.getImgurl());
            orderManager.setCreateTime(orde.getCreateTime());
            orderManager.setUsername(user.getName());
            orderManagers.add(orderManager);
        }
        return orderManagers;
    }

    @Override
    public boolean del(String orderNo) {
        QueryWrapper<Orde> ordeQueryWrapper = new QueryWrapper<>();
        ordeQueryWrapper.eq("orderNo",orderNo);
        Orde orde = this.getOne(ordeQueryWrapper);
        orde.setStatus(0);
        orde.setStatusDesc("已取消");
        this.updateById(orde);
        QueryWrapper<Ordergood> ordergoodQueryWrapper = new QueryWrapper<>();
        ordergoodQueryWrapper.eq("orderNo", orde.getOrderNo());
        Ordergood ordergood = ordergoodService.getOne(ordergoodQueryWrapper);
        ordergood.setStatus(0);
        return ordergoodService.updateById(ordergood);
    }

    @Override
    public boolean rec(String orderNo) {
        QueryWrapper<Orde> ordeQueryWrapper = new QueryWrapper<>();
        ordeQueryWrapper.eq("orderNo",orderNo);
        Orde orde = this.getOne(ordeQueryWrapper);
        orde.setStatus(1);
        orde.setStatusDesc("已支付");
        this.updateById(orde);
        QueryWrapper<Ordergood> ordergoodQueryWrapper = new QueryWrapper<>();
        ordergoodQueryWrapper.eq("orderNo", orde.getOrderNo());
        Ordergood ordergood = ordergoodService.getOne(ordergoodQueryWrapper);
        ordergood.setStatus(1);
        return ordergoodService.updateById(ordergood);
    }

    @Override
    public boolean mulDel(String[] orderNos) {
        try {
            for (String orderNo : orderNos) {
                del(orderNo);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean sendGood(String orderNo) {
        QueryWrapper<Orde> ordeQueryWrapper = new QueryWrapper<>();
        ordeQueryWrapper.eq("orderNo", orderNo);
        //获取订单号
        Orde orde = this.getOne(ordeQueryWrapper);
        orde.setStatus(2);
        orde.setStatusDesc("已发货");
        //修改订单表为已发货
        this.updateById(orde);
        //同步修改订单商品表
        QueryWrapper<Ordergood> ordergoodQueryWrapper = new QueryWrapper<>();
        ordergoodQueryWrapper.eq("orderNo", orderNo);
        Ordergood ordergood = ordergoodService.getOne(ordergoodQueryWrapper);
        ordergood.setStatus(2);
        return ordergoodService.updateById(ordergood);
    }
}
